package com.example.test.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.test.AddCardActivity;
import com.example.test.R;
import com.example.test.demin.Card;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CardFragment extends BaseFragment implements OnClickListener {

	@ViewInject(R.id.lv_cardlist)
	private ListView lv_cardlist;
	@ViewInject(R.id.btn_addcard)
	private Button btn_addcard;

	private List<Card> list = new ArrayList<Card>();
	private CardListAdapter adapter;
	private String cardnumber;

	@Override
	public void initData() {
		checkList();
		if (adapter == null) {

			adapter = new CardListAdapter();

		} else {
			adapter.notifyDataSetChanged();
		}
		lv_cardlist.setAdapter(adapter);
		btn_addcard.setOnClickListener(this);
	}

	private void checkList() {
		// TODO Auto-generated method stub
		DbDaoUtils daoUtils = new DbDaoUtils(ct);
		list = daoUtils.findAllCard();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		checkList();
		if (adapter == null) {

			adapter = new CardListAdapter();

		} else {
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public View initView(LayoutInflater inflater) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.card_layout, null);
		ViewUtils.inject(this, view);

		return view;
	}

	private int[] ids2 = { R.color.gridcolor1, R.color.gridcolor2,
			R.color.gridcolor3, R.color.gridcolor4, R.color.gridcolor5,
			R.color.gridcolor6 };
	
	class CardListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = View.inflate(ct, R.layout.carditem_layout, null);
				holder = new ViewHolder();
				holder.tv_bankname=(TextView) convertView.findViewById(R.id.tv_bank_name);
				holder.tv_cardnumber=(TextView) convertView.findViewById(R.id.tv_card_number);
				holder.ly_card=(LinearLayout) convertView.findViewById(R.id.ly_card);
				convertView.setTag(holder);
			}
			holder.tv_bankname.setText(list.get(position).getBankname());
			holder.tv_cardnumber.setText(list.get(position).getCardnumber());
			int i=position%ids2.length;
		    if(i>=6){
		    	i=6;
		    }
			holder.ly_card.setBackgroundResource(ids2[i]);
			return convertView;
		}

	}

	public static class ViewHolder {
             TextView tv_cardnumber;
             TextView tv_bankname;
             LinearLayout ly_card;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_addcard:
			Intent intent=new Intent(ct, AddCardActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

}
